//
//  ST_PecSquareTabCell.swift
//  btvideo
//
//  Created by mac on 2020/1/14.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit

class ST_PecSquareTabCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        contentView.backgroundColor = .random
        
        contentView.addSubview(left_btn)
        contentView.addSubview(right_btn)
        
        left_btn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
//            make.top.equalToSuperview().offset(32)
            make.bottom.equalToSuperview().offset(-15)
            make.width.equalTo(150)
            make.height.equalTo(35)
        }
        
        right_btn.snp.makeConstraints { (make) in
            make.left.equalTo(left_btn.snp.right).offset(5)
            make.width.height.centerY.equalTo(left_btn)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var selectedColor = Color.white
    
    fileprivate lazy var normaColor = Color.init(hexString: "#333333")!
    
    fileprivate lazy var selectedBackgroundColor = Color.init(hexString: "#FEC207")!
    
    fileprivate lazy var normaBackgroundColor = Color.white
    
    fileprivate lazy var left_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setBackgroundImage(UIImage.init(color: selectedBackgroundColor, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.selected)
        
        control.setBackgroundImage(UIImage.init(color: normaBackgroundColor, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.normal)
        
        control.setTitleColorForAllStates(normaColor)
        
        control.setTitleColor(selectedColor, for: UIControl.State.selected)
        
        control.cornerRadius = 17
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        
        control.addTarget(self, action: #selector(click_handle(sender:)), for: UIControl.Event.touchUpInside)
        
        control.tag = baseTag
        
        return control
    }()
    
    fileprivate lazy var right_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setBackgroundImage(UIImage.init(color: selectedBackgroundColor, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.selected)
        
        control.setBackgroundImage(UIImage.init(color: normaBackgroundColor, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.normal)
        
        control.setTitleColorForAllStates(normaColor)
        
        control.setTitleColor(selectedColor, for: UIControl.State.selected)
        
        control.cornerRadius = 17
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        
        control.addTarget(self, action: #selector(click_handle(sender:)), for: UIControl.Event.touchUpInside)
        
        control.tag = baseTag + 1
        
        return control
    }()
    
    fileprivate let baseTag = 10923
    
    var model = ST_PeculiaritySquareTabCellModel(){
        didSet{
            
            if let m = model.array[safe:0]{
                
                left_btn.setTitleForAllStates(m.str)
                left_btn.isSelected = m.isSelected
            }
            
            if let m = model.array[safe:1]{
                
                right_btn.setTitleForAllStates(m.str)
                right_btn.isSelected = m.isSelected
            }
        }
    }
    
    
    var click_closure:((Int)->())?
    
}


extension ST_PecSquareTabCell{
    
    @objc func click_handle(sender:UIButton){
        
        if sender.isSelected{
            return
        }
        
        
        click_closure?(sender.tag-baseTag)
        
    }
    
}
